<template>
  <div class="app-container">
    <div class="app-head">
      <el-form>
        <el-row type="flex" justify="start">
          <el-col :span="5">
            <el-form-item label="账单编号" label-width="80px">
              <el-input v-model="code" style="width:90%" size="mini" />
            </el-form-item>
          </el-col>

          <el-col :span="5">
            <el-form-item label="订单编号" label-width="80px">
              <el-input v-model="oNumber" style="width:90%" size="mini" />
            </el-form-item>
          </el-col>

          <el-col :span="5">
            <el-form-item label="用户ID" label-width="80px">
              <el-input v-model="userId" style="width:90%" size="mini" />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="产品名称" label-width="100px">
              <el-input v-model="pName" style="width:90%" size="mini" />
            </el-form-item>
          </el-col>
          <el-col :span="2" class="flexd">
            <el-button
              v-powerbtn="{power:'business-accountTable-search'}"
              size="mini"
              round
              type="primary"
              icon="el-icon-search"
              @click="loadList"
            >查询</el-button>
          </el-col>
        </el-row>
        <el-row type="flex" justify="start">
          <el-col :span="5">
            <el-form-item label="账单金额" label-width="80px">
              <el-input ref="vipHeight" v-model="price" style="width:90%" size="mini" />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="账单类型" label-width="80px">
              <el-select v-model="accountType" style="width:90%" size="mini" placeholder="请选择">
                <!-- <el-option v-for="item of pay" :key="item.billingChannelId" :value="item.billingChannelId" :label="item.billingChannelName" /> -->
                <el-option value="" label="全部" />
                <el-option value="0" label="订购" />
                <el-option value="1" label="退订" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="5">
            <el-form-item label="产品编号" label-width="80px">
              <el-input v-model="pCode" style="width:90%" size="mini" />
            </el-form-item>
          </el-col>
          <el-col v-show="platformArr.length > 1" :span="5">
            <el-form-item label="所属业务平台" label-width="100px">
              <el-select v-model="platform" style="width:90%" size="mini" placeholder="请选择">
                <el-option value="" label="全部" />
                <el-option
                  v-for="item of platformArr"
                  :key="item.idVal"
                  :value="item.idVal"
                  :label="item.powerName"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="2" class="flexd">
            <el-button
              size="mini"
              round
              icon="el-icon-refresh"
              @click="reset"
            >
              重置
            </el-button>
          </el-col>
        </el-row>
        <el-row type="flex" justify="start">
          <el-col :span="10">
            <el-form-item label="账单时间" label-width="80px">
              <el-date-picker
                v-model="startDate"
                :picker-options="pickerOptions1"
                :editable="false"
                :style="tiemLen"
                type="datetime"
                size="mini"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss   "
                placeholder="选择开始时间"
                @change="checkStartTime"
              />
              至
              <el-date-picker
                v-model="endDate"
                :picker-options="pickerOptions2"
                :editable="false"
                :style="tiemLen"
                type="datetime"
                size="mini"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss   "
                placeholder="选择结束时间"
                class="timeLength"
                @change="checkTime"
              />
            </el-form-item>
          </el-col>

          <el-col :span="5">
            <el-form-item label="支付渠道" label-width="80px">
              <el-select v-model="payCode" style="width:90%" size="mini" placeholder="请选择">
                <el-option v-for="item of pay" :key="item.billingChannelId" :value="item.billingChannelId" :label="item.billingChannelName" />
              </el-select>
            </el-form-item>
          </el-col>

        </el-row>
      </el-form>
    </div>
    <div class="addBtns">
      <el-button
        v-powerbtn="{power:'business-accountTable-download'}"
        type="primary"
        size="mini"
        round
        icon="el-icon-download"
        @click="downLoad"
      >导出</el-button>
    </div>
    <div class="app-table">
      <el-table
        :data="list"
        border
        style="width: 100%"
      >
        <el-table-column label="序号" width="50">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>

        <el-table-column label="账单时间">
          <template slot-scope="scope">
            <el-popover v-if="scope.row.accountTime" :content="scope.row.accountTime" placement="top" trigger="hover">
              <span slot="reference" style="cursor:pointer">{{ scope.row.accountTime }}</span>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="账单编号">
          <template slot-scope="scope">
            <el-popover v-if="scope.row.code" :content="scope.row.code" placement="top" trigger="hover">
              <span slot="reference" style="cursor:pointer">{{ scope.row.code }}</span>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="订单编号">
          <template slot-scope="scope">
            <el-popover v-if="scope.row.oNumber" :content="scope.row.oNumber" placement="top" trigger="hover">
              <span slot="reference" style="cursor:pointer">{{ scope.row.oNumber }}</span>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="产品编号">
          <template slot-scope="scope">
            <el-popover v-if="scope.row.pCode" :content="scope.row.pCode" placement="top" trigger="hover">
              <span slot="reference" style="cursor:pointer">{{ scope.row.pCode }}</span>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="产品名称">
          <template slot-scope="scope">
            <el-popover v-if="scope.row.pName" :content="scope.row.pName" placement="top" trigger="hover">
              <span slot="reference" style="cursor:pointer">{{ scope.row.pName }}</span>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="用户ID">
          <template slot-scope="scope">
            <el-popover v-if="scope.row.userId" :content="scope.row.userId" placement="top" trigger="hover">
              <span slot="reference" style="cursor:pointer">{{ scope.row.userId }}</span>
            </el-popover>
          </template>
        </el-table-column>

        <el-table-column label="账单金额">
          <template slot-scope="scope">
            <el-popover v-if="scope.row.price" :content="scope.row.price" placement="top" trigger="hover">
              <span slot="reference" style="cursor:pointer">{{ scope.row.price }}</span>
            </el-popover>
          </template>
        </el-table-column>

        <el-table-column label="原价">
          <template slot-scope="scope">
            <el-popover v-if="scope.row.constPrice" :content="scope.row.constPrice" placement="top" trigger="hover">
              <span slot="reference" style="cursor:pointer">{{ scope.row.constPrice }}</span>
            </el-popover>
          </template>
        </el-table-column>

        <el-table-column label="所属平台">
          <template slot-scope="scope">
            <el-popover v-if="scope.row.platName" :content="scope.row.platName" placement="top" trigger="hover">
              <span slot="reference" style="cursor:pointer">{{ scope.row.platName }}</span>
            </el-popover>
          </template>
        </el-table-column>

        <el-table-column label="支付方式">
          <template slot-scope="scope">
            {{ scope.row.payCode }}
          </template>
        </el-table-column>

        <el-table-column label="账单类型">
          <template slot-scope="scope">
            <div v-if="scope.row.accountType == 0">
              订购
            </div>

            <div v-if="scope.row.accountType == 1">
              退订
            </div>
          </template>
        </el-table-column>

        <!-- <el-table-column label="备注" width="100">
          <template slot-scope="scope">
            <el-button
              type="primary"
              plain
              size="mini"
              icon="el-icon-third-tongyong"
              @click="watch(scope.row.remark)"
            >查看</el-button>
          </template>
        </el-table-column> -->
      </el-table>
      <div class="pagination-container">
        <el-pagination :current-page="pageid" :page-sizes="[20,50,100]" :page-size="pagecount" :total="count" background highlight-current-row layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
      </div>
    </div>
    <watch-info :watch-dialog="watchDialog" :watch-id="watchId" @closeWatch="closeWatch" />
  </div>
</template>

<script>
import { getList } from '@/api/billList.js'
import { selectDown } from '@/api/expenses.js'
import { download } from '@/utils/download.js'
import { mapGetters } from 'vuex'
import powerbtn from '@/directive/powerbtn/index.js'
import watchInfo from './components/watch.vue'
export default {
  directives: {
    powerbtn
  },
  components: {
    watchInfo: watchInfo
  },
  data() {
    return {
      tiemLen: '',
      list: [], // 请求列表
      payChannel: [],
      code: '', // 账单编号
      oNumber: '', // 订单编号
      userId: '', // 用户编号
      price: '', // 价格
      accountType: '', // 账单类型
      pCode: '', // 产品编号
      payMode: '', // 支付渠道code
      platform: '', // 所属业务平台
      pName: '', // 产品名称
      platformArr: [],
      payCode: '', // 支付编号
      pay: [], // 支付渠道下拉
      pageid: 1, // 当前页数
      pagecount: 20, // 每页可显示条数
      status: false, // 请求状态
      watchDialog: false, // 查看遮罩
      editDialog: false, // 编辑遮罩
      watchId: '', // 策略id
      count: 0, // 总数量
      addDialog: false, // 新增遮罩
      paySelect: [],
      pickerOptions1: {
        disabledDate(time) {
          return time.getTime() > Date.now()
        }
      },
      pickerOptions2: {
        disabledDate(time) {
          return time.getTime() > Date.now()
        }
      },
      startDate: '',
      endDate: ''
    }
  },
  computed: {
    ...mapGetters(['plat'])
  },
  mounted() {
    this.$nextTick(() => {
      var len = this.$refs.vipHeight.$el.offsetWidth
      console.log(len)
      this.tiemLen = {
        'width': len + 'px'
      }
    })
    this.platformArr = this.plat
    if (this.platformArr.length > 1) {
      this.platformArr.forEach(item => {
        var len = item.powerCode.lastIndexOf('-')
        item.idVal = item.powerCode.substr(len + 1)
        this.payChannel.push(item.idVal)
      })
      this.getSelect()
    }
    if (this.platformArr.length === 1) {
      var sina = this.platformArr[0].powerCode.lastIndexOf('-')
      console.log(this.platformArr[0])
      this.platformId = this.platformArr[0].powerCode.substr(sina + 1)
      this.payChannel = this.platformArr[0].powerCode.substr(sina + 1)
      this.getSelect(true)
    }
  },
  methods: {
    // 请求列表
    loadList(status) {
      this.pageid = 1
      if (status === true) {
        this.platform = this.platformId
      }
      const request = {
        code: this.code,
        oNumber: this.oNumber,
        userId: this.userId,
        price: this.price,
        accountType: this.accountType,
        payCode: this.payCode,
        startAccountTime: this.startDate,
        endAccountTime: this.endDate,
        pCode: this.pCode,
        pName: this.pName,
        platformId: this.platform,
        pageNum: 1,
        limit: 20
      }
      getList(request).then(res => {
        if (res.resultCode === '0000') {
          this.list = res.data.list
          this.count = parseInt(res.data.total) // 总数量
          // if (this.list.length > 0) {
          // this.pageid = parseInt(res.data.list[0].pageNum) // 当前页数
          // }
          this.status = true
        }
      }).catch(() => {
        this.$message({
          message: '请求列表失败',
          type: 'error'
        })
      })
    },
    // 校验开始时间
    checkStartTime(time) {
      const reg = new RegExp('-', 'g')
      // 比较是否大于现在的时间
      var selectTime
      if (!!time) { // eslint-disable-line
        selectTime = new Date(time.replace(reg, '/')).getTime() // 所选时间
      } else {
        selectTime = ''
      }
      const nowTime = Date.now() // 现在的时间
      if (selectTime > nowTime) {
        this.$alert('所选时间不能大于当前时间！', '提示', {
          confirmButtonText: '确定',
          callback: action => {
            this.startDate = ''
          }
        })
      } else {
        if (!!time) {  // eslint-disable-line
          var end
          if (this.endDate !== null) {
            end = new Date(this.endDate.replace(reg, '/')).getTime()
          } else {
            end = ''
          }
          var first = new Date(time.replace(reg, '/')).getTime()
          if (!!end && first > end) {
            this.$message({
              message: '开始时间不能大于结束时间',
              type: 'error'
            })
            this.startDate = ''
          }
        }
      }
    },

    // 校验结束时间
    checkTime(time) {
      const reg = new RegExp('-', 'g')
      // console.log(newStr)
      var selectTime
      if (!!time) { // eslint-disable-line
        selectTime = new Date(time.replace(reg, '/')).getTime() // 所选时间
      } else {
        selectTime = ''
      }
      const nowTime = Date.now() // 现在的时间
      if (selectTime > nowTime) {
        this.$alert('所选时间不能大于当前时间！', '提示', {
          confirmButtonText: '确定',
          callback: action => {
            this.endDate = ''
          }
        })
      } else {
        if (!!time) {  // eslint-disable-line
          var start
          if (this.startDate !== null) {
            start = new Date(this.startDate.replace(reg, '/')).getTime()
          } else {
            start = ''
          }
          var end = new Date(time.replace(reg, '/')).getTime()
          if (end < start) {
            this.$message({
              message: '开始时间不能大于结束时间',
              type: 'error'
            })
            this.endDate = ''
          }
        }
      }
    },
    // 请求支付渠道下拉
    getSelect(status) {
      selectDown({ platformId: Array.isArray(this.payChannel) ? this.payChannel.join(',') : this.payChannel }).then(res => {
        if (res.resultCode === '0000') {
          this.pay = res.data.list
          this.loadList(status)
        }
      })
    },
    downLoad() {
      var request = {
        code: this.code,
        oNumber: this.oNumber,
        userId: this.userId,
        price: this.price,
        accountType: this.accountType,
        pCode: this.pCode,
        startAccountTime: this.startDate,
        endAccountTime: this.endDate,
        payCode: this.payCode,
        pName: this.pName
      }
      download(request, '/orderManage/accountListDownload.do', '账单列表')
    },
    reset() {
      if (this.platformArr.length > 1) {
        this.platform = ''
      }
      this.code = ''
      this.oNumber = ''
      this.userId = ''
      this.price = ''
      this.accountType = ''
      this.payCode = ''
      this.startAccountTime = ''
      this.endAccountTime = ''
      this.pCode = ''
      this.startDate = ''
      this.endDate = ''
      this.pName = ''
    },

    // 当前页面显示条数改变时
    handleSizeChange(num) {
      this.flag = false
      const request = {
        code: this.code,
        oNumber: this.oNumber,
        userId: this.userId,
        price: this.price,
        accountType: this.accountType,
        payCode: this.payCode,
        startAccountTime: this.startDate,
        endAccountTime: this.endDate,
        pCode: this.pCode,
        pName: this.pName,
        platformId: this.platform,
        pageNmu: 1, // 当前页数
        limit: num // 每页可显示条数
      }
      console.log('请求数据')
      console.log(request)
      getList(request).then(res => {
        this.list = res.data.list // 列表项
        this.count = parseInt(res.data.total) // 总数量
        this.pageid = 1
        this.pagecount = num
        this.flag = true
      }).catch(() => {
        this.$message({
          message: '请求列表失败，请重试',
          type: 'error'
        })
      })
    },

    // 跳转某页
    handleCurrentChange(num) {
      this.pageid = num // 当前页数
      console.log(num)
      const request = {
        code: this.code,
        oNumber: this.oNumber,
        userId: this.userId,
        price: this.price,
        accountType: this.accountType,
        payCode: this.payCode,
        startAccountTime: this.startDate,
        endAccountTime: this.endDate,
        pCode: this.pCode,
        pName: this.pName,
        platformId: this.platform,
        pageNum: num, // 当前页数
        limit: this.pagecount // 每页可显示条数
      }
      console.log('请求数据')
      console.log(request)
      // if (this.flag === true) {
      getList(request).then(response => {
        this.list = response.data.list // 列表项
        this.count = parseInt(response.data.total) // 总数量
        console.log('返回数据')
        console.log(response.data)
      })
      // }
    },

    // 查看
    watch(remark) {
      this.watchDialog = true
      this.watchId = remark
    },

    // 关闭查看回调
    closeWatch() {
      this.watchDialog = false
    },

    // 新增
    add() {
      this.addDialog = true
      this.paySelect = this.pay
    },

    // 关闭新增回调
    closeAdd() {
      this.addDialog = false
    },

    closeEdit() {
      this.editDialog = false
    }
  }
}
</script>

<style scoped>
.addBtns{
  margin: 5px 0;
}
.timeLength{
  width: 37.5% !important;
}
</style>
